/**
 * @file test_alarm.c
 * @author liangwenhao (1943080020@qq.com)
 * @brief 
 * @version 0.1
 * @date 2021-07-30
 * 
 * @copyright Copyright (c) 2021
 * 
 * @authorGitee  https://gitee.com/liangwenhao
 * @authorGithub https://github.com/WHaoL
 * @authorBlogs https://blog.csdn.net/liangwenhao1108
 *  
 * @Code Description: P268 测试alarm()是否是阻塞函数  
 *  
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>

void alarm_handler(int signo)
{
    if (signo == SIGALRM)
        printf("signal = SIGALRM\n");
    else
        printf("signal = %d\n", signo);
}

int main(void)
{
    signal(SIGALRM, alarm_handler);
    int i = 0;

    alarm(3); // alarm()只是启动了一个定时器，不影响程序的执行，不阻塞进程

    printf("start time = %ld\n", time(NULL));
    // while (i < 2) //  不会卡在pause()处
    while (i < 3)// 会导致卡在pause()处
    // while (i < 10)// 会导致卡在pause()处
    {
        printf("%d second\n", i++);
        sleep(1);
    }
    printf("end   time = %ld\n", time(NULL));

    pause();

    printf("success\n");

    exit(0);
}